angular.module("app", ["ui.router", "ngSanitize", "ngResource", "ui.bootstrap", "dx"]);
angular.module("app").config(["$stateProvider", "$urlRouterProvider", "$locationProvider", "$httpProvider", function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.transformResponse = function (data, headersGetter, status) {
        if (status !== 200) {
            alert("服务器开了小差, 请刷新或联系管理员");
            sessionStorage.clear();
            location.href = "#/login";
            return;
        }
        if (headersGetter()["content-type"] == "application/json;charset=UTF-8") {
            data = angular.fromJson(data);
            if (location.hash == "#/login") return data;
            if (data.statusCode == -100) {
                alert("系统服务连接异常!");
                location.href = "#/login";
            } else if (data.statusCode == -1) {
                alert("用户未登录");
                location.href = "#/login";
            }
        }
        return data;
    };
    $urlRouterProvider.when("", "/login");
    $urlRouterProvider.otherwise("/home/404");
    $stateProvider
        .state("login", {
            url: "/login",
            views: {
                "mainContent": {
                    templateUrl: "template/login/login.html?tk=" + new Date().getTime(),
                    controller: "loginCtrl"
                }
            }
        })
        .state("home", {
            url: "/home",
            views: {
                "mainContent": {
                    templateUrl: "template/home/home.html?tk=" + new Date().getTime(),
                    controller: "homeCtrl"
                }
            },
            resolve: {
                // "treeViewDataSource": ["User", function (User) {
                //     return User.getTreeData({}, {test: 1}, function (data) {
                //     }).$promise;
                // }],
                "qiniuToken": ["User", function (User) {
                    return User.getConfig({}, {orgId: JSON.parse(sessionStorage.userInfo).orgId}, function (data) {
                    }).$promise;
                }]
            }
        })
        .state("resetPassword", {
            url: "/resetPassword",
            views: {
                "mainContent": {
                    templateUrl: "template/resetPassword/resetPassword.html?tk=" + new Date().getTime(),
                    controller: "resetPassword.Ctrl"
                }
            }
        })
        .state("home.welcome", {
            url: "/welcome",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/welcome.html?tk=" + new Date().getTime(),
                    controller: "welcomeCtrl"
                }
            }
        })
        // .state("home.setting", {
        //     url: "/setting",
        //     views: {
        //         "viewContent": {
        //             templateUrl: "template/home/setting/setting.html?tk=" + new Date().getTime(),
        //             controller: "settingCtrl"
        //         }
        //     }
        // })
        // .state("home.org", {
        //     url: "/org",
        //     abstract: true,
        //     views: {
        //         "viewContent": {
        //             template: "<div ui-view='subview'><div/>"
        //         }
        //     }
        // })
        // .state("home.org.list", {
        //     url: "/list",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/org/list/list.html?tk=" + new Date().getTime(),
        //             controller: "org.list.Ctrl"
        //         }
        //     }
        // })
        // .state("home.org.info", {
        //     url: "/info",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/org/info/info.html?tk=" + new Date().getTime(),
        //             controller: "org.info.Ctrl"
        //         }
        //     }
        // })
        // .state("home.org.managerList", {
        //     url: "/managerList",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/org/managerList/managerList.html?tk=" + new Date().getTime(),
        //             controller: "org.managerList.Ctrl"
        //         }
        //     }
        // })
        .state("home.welfare", {
            url: "/welfare",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.welfare.list", {
            url: "/list",
            views: {
                "subview": {
                    templateUrl: "template/home/welfare/list/list.html?tk=" + new Date().getTime(),
                    controller: "welfare.list.Ctrl"
                }
            }
        })
        .state("home.welfare.info", {
            url: "/info/:type/:id",
            params: { welfareInfo: null },
            views: {
                "subview": {
                    templateUrl: "template/home/welfare/info/info.html?tk=" + new Date().getTime(),
                    controller: "welfare.info.Ctrl"
                }
            }
        })
        .state("home.welfare.welfareItemList", {
            url: "/welfareItemList",
            views: {
                "subview": {
                    templateUrl: "template/home/welfare/welfareItemList/welfareItemList.html?tk=" + new Date().getTime(),
                    controller: "welfare.welfareItemList.Ctrl"
                }
            }
        })
        // .state("home.group.verify", {
        //     url: "/verify",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/group/verify/verify.html?tk=" + new Date().getTime(),
        //             controller: "group.verify.Ctrl"
        //         }
        //     }
        // })
        // .state("home.group.volunteerList", {
        //     url: "/volunteerList/:groupId",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/group/volunteerList/volunteerList.html?tk=" + new Date().getTime(),
        //             controller: "group.volunteerList.Ctrl"
        //         }
        //     }
        // })
        // .state("home.group.activityList", {
        //     url: "/activityList/:userId",
        //     views: {
        //         "subview": {
        //             templateUrl: "template/home/group/activityList/activityList.html?tk=" + new Date().getTime(),
        //             controller: "group.activityList.Ctrl"
        //         }
        //     }
        // })
        .state("home.user", {
            url: "/user",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.user.changePassword", {
            url: "/changePassword",
            views: {
                "subview": {
                    templateUrl: "template/home/user/changePassword/changePassword.html?tk=" + new Date().getTime(),
                    controller: "user.changePassword.Ctrl"
                }
            }
        })
        .state("home.user.manageList", {
            url: "/manageList",
            views: {
                "subview": {
                    templateUrl: "template/home/user/manageList/manageList.html?tk=" + new Date().getTime(),
                    controller: "user.manageList.Ctrl"
                }
            }
        })
        .state("home.user.manageInfo", {
            url: "/manageInfo/:type/:manageId",
            params: { manageInfo: null },
            views: {
                "subview": {
                    templateUrl: "template/home/user/manageInfo/manageInfo.html?tk=" + new Date().getTime(),
                    controller: "user.manageInfo.Ctrl"
                }
            }
        })
        .state("home.topic", {
            url: "/topic",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.topic.list", {
            url: "/list",
            views: {
                "subview": {
                    templateUrl: "template/home/topic/list/list.html?tk=" + new Date().getTime(),
                    controller: "topic.list.Ctrl"
                }
            }
        })
        .state("home.topic.info", {
            url: "/info/:id",
            views: {
                "subview": {
                    templateUrl: "template/home/topic/info/info.html?tk=" + new Date().getTime(),
                    controller: "topic.info.Ctrl"
                }
            }
        })
        .state("home.topic.comments", {
            url: "/comments/:id",
            views: {
                "subview": {
                    templateUrl: "template/home/topic/comments/comments.html?tk=" + new Date().getTime(),
                    controller: "topic.comments.Ctrl"
                }
            }
        })
        ;
    $httpProvider.interceptors.push("timestampMarker");
}]);